package com.sgb.zhaocai.view.demand.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.sgb.zhaocai.BR;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.entity.DemandForecastListEntity;
import com.sgb.zhaocai.entity.GoodsHeaderDTO;
import com.sgb.zhaocai.entity.GoodsListDTO;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/8/16
 * AUTHOR:F_Q
 * Description:
 */
public class DemandListTwoAdapter extends BaseRecycleAdapter<GoodsListDTO> {

    private int headerCount;

    public DemandListTwoAdapter(Context context, DemandForecastListEntity entity) {
        super(context, R.layout.zc_demand_list_two, new ArrayList<>());
        if (entity.getGoodsHeader() != null && entity.getGoodsHeader().size() > 0) {
            for (GoodsHeaderDTO goodsHeaderDTO : entity.getGoodsHeader()) {
                GoodsListDTO dto = new GoodsListDTO();
                dto.setName(goodsHeaderDTO.getName());
                dto.setTranslate(goodsHeaderDTO.getTranslate());
                dto.setValue(goodsHeaderDTO.getTranslate());
                datas.add(dto);
            }
        }
        for (int i = 0; i < entity.getGoodsList().size(); i++) {
            List<GoodsListDTO> goodsListDTOS = entity.getGoodsList().get(i);
            if (goodsListDTOS != null && goodsListDTOS.size() > 0) {
                for (GoodsListDTO goodsListDTO : goodsListDTOS) {
                    datas.add(goodsListDTO);
                }
            }
        }
        headerCount = entity.getGoodsHeader().size();
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        TextView tv = holder.itemView.findViewById(R.id.tv_title);
        if (headerCount > position) {
            tv.setText(datas.get(position).getTranslate());
            tv.setTextSize(13);
            tv.setTextColor(context.getResources().getColor(R.color.color_black));
        } else {
            if (TextUtils.isEmpty(datas.get(position).getValue())) {
                tv.setText("/");
            } else {
                if (TextUtils.equals(datas.get(position).getName(),"planUseTime")){
                    tv.setText(DateUtils.timestampToDate(Long.parseLong(datas.get(position).getValue()))+"");
                }else {
                    tv.setText(String.valueOf(datas.get(position).getValue()));
                }
            }
            tv.setTextSize(12);
            tv.setTextColor(context.getResources().getColor(R.color.color_tab_two));
        }

    }

    @Override
    public void convert(ItemViewHolder holder, GoodsListDTO entity) {
        holder.setBinding(BR.viewModel, entity.getValue());
    }


}
